package com.x.erp.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.x.erp.model.Car;
import com.x.erp.model.CarFavorite;
import com.x.framework.dao.BaseDao;
/**
 * 
 * 车辆收藏dao
 * @author pansong
 *
 */
@Repository
public class CarFavoriteDao extends BaseDao {

	private final static String selectCarFavoriteListSql = "SELECT " +
			"ec.*, " +
			"bb.BRAND_NAME, " +
			"bs.SERIES_NAME, " +
			"em.MARKET_NAME, " +
			"er.REGION_NAME, " +
			"es.SHOP_NAME, " +
			"es.SHOP_MOBILE, " +
			"es.SHOP_ADDRESS, " +
			"es.SHOP_LEGAL_NAME, " +
			"es.SHOP_LEGAL_MOBILE, " +
			"es.SHOP_PARK_NUM, " +
			"es.SHOP_CAR_NUM, " +
			"es.SHOP_MANAGE_NAME, " +
			"es.SHOP_MANAGE_MOBILE, " +
			"bc.CITY_NAME, " +
			"eu.USER_NAME, "+
			"eu.USER_MOBILE, "+
			"ecs.SALE_CREATE_TIME, "+
			"eco.ORDER_CREATE_TIME "+
			"FROM " +
			"ERP_CAR_FAVORITE ecf  " +
			"JOIN ERP_CAR ec ON ecf.CAR_ID = ec.CAR_ID " +
			"JOIN x_base.BASE_BRAND bb ON ec.BRAND_ID = bb.BRAND_ID " +
			"JOIN x_base.BASE_SERIES bs ON ec.SERIES_ID = bs.SERIES_ID " +
			"JOIN ERP_MARKET em ON ec.MARKET_ID = em.MARKET_ID " +
			"JOIN ERP_SHOP es ON ec.SHOP_ID = es.SHOP_ID " +
			"LEFT JOIN ERP_USER eu ON ec.SALE_USER_ID = eu.USER_ID "+
			"LEFT JOIN ERP_REGION er ON ec.REGION_ID = er.REGION_ID " +
			"LEFT JOIN ERP_CAR_SALE ecs ON ec.CAR_ID = ecs.CAR_ID "+
			"LEFT JOIN ERP_CAR_ORDER eco ON ec.CAR_ID = eco.CAR_ID " +
			"LEFT JOIN base_city bc ON bc.city_id = em.city_id " +
			"WHERE 1=1 ";
	/**
	 * 查询收藏列表
	 * @param carFavorite
	 * @return
	 * @throws Exception
	 */
	public List<Car> selectCarFavoriteList(CarFavorite carFavorite) throws Exception{
		StringBuilder sql = new StringBuilder(selectCarFavoriteListSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, carFavorite);
		sql.append(" ORDER BY ecf.FAVORITE_CREATE_TIME DESC ");
		return this.queryForList(sql.toString(), Car.class, params, carFavorite);
	}
	
	private final static String selectCarFavoriteSql = "SELECT " +
			"ecf.* " +
			"FROM " +
			"ERP_CAR_FAVORITE ecf  " +
			"WHERE 1=1 ";
	/**
	 * 查询收藏信息
	 * @param carFavorite
	 * @return
	 * @throws Exception
	 */
	public CarFavorite selectCarFavorite(CarFavorite carFavorite) throws Exception{
		StringBuilder sql = new StringBuilder(selectCarFavoriteSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, carFavorite);
		sql.append(" ORDER BY ecf.FAVORITE_CREATE_TIME DESC ");
		List<CarFavorite> list = this.queryForList(sql.toString(), CarFavorite.class, params);
		if(list !=null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}
	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param carCollection
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, CarFavorite carFavorite) {
		if(carFavorite != null) {
			if(StringUtils.isNotEmpty(carFavorite.getCarFavoriteId())){
				sql.append("AND ecf.CAR_FAVORITE_ID = ? ");
				params.add(carFavorite.getCarFavoriteId());
			}
			if(StringUtils.isNotEmpty(carFavorite.getUserId())){
				sql.append("AND ecf.USER_ID = ? ");
				params.add(carFavorite.getUserId());
			}
			if(StringUtils.isNotEmpty(carFavorite.getCarId())){
				sql.append("AND ecf.CAR_ID = ? ");
				params.add(carFavorite.getCarId());
			}
		}
	}
}
